﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Collections.ObjectModel;
using System.Data;
using System.Windows.Media.Imaging;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Controls;
using System.Globalization;
using produccion.modelo;
using produccion.AccesoDatos;
using produccion.clases;
using System.Windows.Input;
using System.Collections.Specialized;

namespace produccion.vistamodelo
{
    class VMConsultaUsodeMateriales : ViewModelBase
    {
        #region Campos
        #endregion

        #region Constructor
            public VMConsultaUsodeMateriales()
            {
                try
                {
                    if (MDOgeneral.conexion == null || MDOgeneral.conexion.State == ConnectionState.Closed)
                    {
                        MDOgeneral.conectar();
                    }
                    strTituloVentana = "Consulta Uso de Materiales - Usuario: " + MDOgeneral.usuario + " - [FRMConsultaUsodeMateriales]";
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        #endregion

        #region Propiedades

            private string _strMaterial;
            public string strMaterial
            {
                get { return _strMaterial; }
                set 
                { 
                    _strMaterial = value;
                    if (_strMaterial != null)
                    {
                        if (_strMaterial.Length == 10)
                            consultaMaterial();
                    }
                    base.OnPropertyChanged("strMaterial"); 
                }
            }

            private string _strNombre;
            public string strNombre
            {
                get { return _strNombre; }
                set { _strNombre = value; base.OnPropertyChanged("strNombre"); }
            }

            private string _strColor;
            public string strColor
            {
                get { return _strColor; }
                set { _strColor = value; base.OnPropertyChanged("strColor"); }
            }

            private string _strNombreColor;
            public string strNombreColor
            {
                get { return _strNombreColor; }
                set { _strNombreColor = value; base.OnPropertyChanged("strNombreColor"); }
            }

            private DataTable _informe = new DataTable();
            public DataTable informe
            {
                get { return _informe; }
                set { _informe = value; base.OnPropertyChanged("informe"); }
            }

            private int _index = 0;
            public int index
            {
                get { return _index; }
                set 
                { 
                    _index = value;
                    if (_index != 0 && _index != -1)
                    {
                        visorFotoReferencia.strCodigo = informe.Rows[_index]["CODREF_TMP"].ToString();
                        visorFotoReferencia.strColor = informe.Rows[_index]["CODCOL_TMP"].ToString();
                    }
                    base.OnPropertyChanged("index"); 
                }
            }

            private string _strTituloVentana;
            public string strTituloVentana
            {
                get { return _strTituloVentana; }
                set { _strTituloVentana = value; base.OnPropertyChanged("strTituloVentana"); }
            }

            private VMvisorFotoReferencia _visorFotoReferencia = new VMvisorFotoReferencia();
            public VMvisorFotoReferencia visorFotoReferencia
            {
                get { return _visorFotoReferencia; }
                set { _visorFotoReferencia = value; base.OnPropertyChanged("visorFotoReferencia"); }
            }


            private List<string> _listamateriales = new List<string>();
            public List<string> listamateriales
            {
                get { return _listamateriales; }
                set { _listamateriales = value; base.OnPropertyChanged("listamateriales"); }
            }

            
        #endregion

        #region Comandos

            RelayCommand _cmdBuscar;
            public ICommand cmdBuscar
            {
                get
                {
                    if (_cmdBuscar == null)
                    {
                        _cmdBuscar = new RelayCommand(r => buscar());
                    }
                    return _cmdBuscar;
                }
            }

            RelayCommand _cmdLimpiar;
            public ICommand cmdLimpiar
            {
                get
                {
                    if (_cmdLimpiar == null)
                    {
                        _cmdLimpiar = new RelayCommand(r => limpiar());
                    }
                    return _cmdLimpiar;
                }
            }

            RelayCommand _cmdSalir;
            public ICommand cmdSalir
            {
                get
                {
                    if (_cmdSalir == null)
                    {
                        _cmdSalir = new RelayCommand(Salir);
                    }
                    return _cmdSalir;
                }
            }

        #endregion

        #region Metodos

            
            public void buscar()
            {
                try
                {
                    FRMBuscarCol frm = new FRMBuscarCol();
                    frm.RequiereColor = true;
                    frm.EsReferencia = false;
                    frm.ShowDialog();
                    if (!String.IsNullOrEmpty(frm.Retorno))
                    {
                        Mouse.OverrideCursor = Cursors.Wait;

                        strMaterial = frm.Retorno.Substring(0, 6);
                        strColor = frm.Retorno.Substring(6,4);

                        CLSMAEMTP mtp = new CLSMAEMTP { CODMAT_MTP = strMaterial, CODCOL_MTP = strColor };
                        mtp.ConsultaMaterial();
                        strNombre = mtp.NOMMATERIAL;
                        strNombreColor = mtp.NOMCOLOR;

                        if (strNombreColor != null)
                            consultar(strMaterial, strColor);

                        Mouse.OverrideCursor = Cursors.Arrow;
                    }
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }

            public void consultaMaterial()
            {
                try
                {
                    Mouse.OverrideCursor = Cursors.Wait;

                    CLSMAEMTP mtp = new CLSMAEMTP { CODMAT_MTP = strMaterial.Substring(0, 6), CODCOL_MTP = strMaterial.Substring(6, 4) };
                    mtp.ConsultaMaterial();
                    strMaterial = mtp.CODMAT_MTP;
                    strColor = mtp.CODCOL_MTP;
                    strNombre = mtp.NOMMATERIAL;
                    strNombreColor = mtp.NOMCOLOR;

                    if (mtp.EXISTE)
                        consultar(strMaterial, strColor);
                    else
                    {
                        MessageBox.Show("El material ingresado no existe.", "Material o color no existe", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                    }

                    Mouse.OverrideCursor = Cursors.Arrow;
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }

            public void consultar(string strMat, string strCol)
            {
                try
                {
                    MDOgeneral mod = new MDOgeneral();

                    MDOgeneral.spComando.CommandText = "SP_USOMAT";
                    MDOgeneral.spComando.Connection = MDOgeneral.conexion;
                    MDOgeneral.spComando.CommandType = CommandType.StoredProcedure;
                    MDOgeneral.spComando.Parameters.Clear();

                    //Input
                    MDOgeneral.spComando.Parameters.Add("pva_codmat", Oracle.DataAccess.Client.OracleDbType.Varchar2, 40).Value = strMat;
                    MDOgeneral.spComando.Parameters["pva_codmat"].Direction = ParameterDirection.Input;

                    MDOgeneral.spComando.Parameters.Add("pva_codcol", Oracle.DataAccess.Client.OracleDbType.Varchar2, 40).Value = strCol;
                    MDOgeneral.spComando.Parameters["pva_codcol"].Direction = ParameterDirection.Input;

                    //Output
                    MDOgeneral.spComando.Parameters.Add("pva_result", Oracle.DataAccess.Client.OracleDbType.Varchar2, 2000).Direction = ParameterDirection.Output;
                    MDOgeneral.spComando.Parameters.Add("pva_errusu", Oracle.DataAccess.Client.OracleDbType.Varchar2, 2000).Direction = ParameterDirection.Output;
                    MDOgeneral.spComando.ExecuteNonQuery();
                    string c = MDOgeneral.spComando.Parameters["pva_result"].Value.ToString();

                    informe.Clear();
                    informe = mod.consulta("select * from tmpusomat");
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }

            public void limpiar()
            {
                try
                {
                    informe.Clear();
                    index = 0;
                    strMaterial = null;
                    strColor = null;
                    strNombre = null;
                    strNombreColor = null;
                    if (visorFotoReferencia != null)
                    {
                        visorFotoReferencia = null;
                    }
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }

            public void Salir(Object frmformulario)
            {
                ((Window)frmformulario).Close();
            }

        #endregion
    }
}
